package com.bidevalution.controller.task;

import com.bidevalution.business.service.JdbcTemplateService;
import com.google.common.collect.Maps;
import com.qtp.core.util.DateUtil;
import lombok.extern.log4j.Log4j2;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @description: 定时删除临时表数据（目前主要用于清单临时数据的删除）
 * @author: xiayf
 * @date: 2019-12-20 18:54
 */
@Log4j2
@Component
public class DeleteTempTableDataTask {

    @Autowired
    private JdbcTemplateService jdbcTemplateService;

    private Logger log = Logger.getLogger(getClass());

    @Scheduled(cron = "0 0 22 1/1 * ? ")
//    @Scheduled(cron = "0 55 20 1/1 * ? ")
    public void execute() {
        try {
            //删除评审结束的标段
            String querySql = "SELECT section_name,td_projectId FROM be_section_info WHERE review_state='3'";
            List<Map<String, Object>> dataList = jdbcTemplateService.selectForList(querySql, null);
            if (dataList != null && !dataList.isEmpty()) {
                String deleteSql1 = "DELETE FROM temp_zbbudget where td_projectid=:tdProjectId";
                String deleteSql2 = "DELETE FROM temp_tbbudget where td_projectid=:tdProjectId";
                String deleteSql3 = "DELETE FROM temp_zbmeasures where td_projectid=:tdProjectId";
                String deleteSql4 = "DELETE FROM temp_tbmeasures where td_projectid=:tdProjectId";
                Map<String, Object> paramMap = Maps.newHashMap();
                for (Map<String, Object> dataMap : dataList) {
                    paramMap.put("tdProjectId", dataMap.get("td_projectId"));
                    log.info("定时删除临时表数据（目前主要用于清单临时数据的删除）开始，标段名称[" + dataMap.get("section_name") + "]，" +
                            "开始时间[" + DateUtil.getCurrentDateStr("yyyy-MM-dd HH:mm:ss") + "]");
                    jdbcTemplateService.update(deleteSql1, paramMap);
                    jdbcTemplateService.update(deleteSql2, paramMap);
                    jdbcTemplateService.update(deleteSql3, paramMap);
                    jdbcTemplateService.update(deleteSql4, paramMap);
                    log.info("定时删除临时表数据（目前主要用于清单临时数据的删除）结束，标段名称[" + dataMap.get("section_name") + "]，" +
                            "结束间[" + DateUtil.getCurrentDateStr("yyyy-MM-dd HH:mm:ss") + "]");
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            log.error("定时删除临时表数据（目前主要用于清单临时数据的删除）异常");
        }
    }

}
